
#百度翻译post请求
import urllib.request
import urllib.parse

url = 'https://fanyi.baidu.com/v2transapi?from=en&to=zh'

headers = {
    # 'pragma': 'no-cache',
    # 'cache-control': 'no-cache',
    # 'sec-ch-ua': '"googLe chrome";v="89", "chromium";v="89", ";NOt a brand";v="99"',
    # 'accept': '*/*',
    # 'X-requested-with': ' XmlhTtprequest',
    # 'sec-ch-ua-mobile': '?0',
    'user-agent': 'mozilla/5.0 (windoWS NT 10.0; Win64; x64) apPleWebKit/537.36 (KHTMl, liKe geckO) chrome/89.0.4389.90 safari/537.36',
    # 'content-type': ' application/x-www-form-urlencoded; charsET=UTF-8',
    # 'origin': 'https://fanyi.baidu.com',
    # 'sec-fetch-site': 'same-origin',
    # 'sec-fetch-mode': 'cors',
    # 'sec-fetch-dest': 'empty',
    # 'referer': 'https://fanyi.baidu.com/?aldtype=16047',
    # 'accept-encoding': ' gzip, deflate, br',
    # 'accept-language': 'ZH-cn,zh;q=0.9',
    'cookie': 'BIDUPSID=347CC3B8FA5BAEDB3E864E9F70022D26; PSTM=1631335016; BAIDUID=347CC3B8FA5BAEDBB322535BBAF00F08:FG=1; BAIDUID_BFESS=347CC3B8FA5BAEDBB322535BBAF00F08:FG=1; DelPer=0; H_PS_PSSID=34439_34380_34145_31660_33848_34524_34584_34092_26350_34555; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; PSINO=1; BA_HECTOr=0l2081a4a58421a4hf1gjod8d0R; hm_lvt_64ecd82404c51e03dc91cb9e8c025574=1631335694; __yjs_duid=1_b4b00e46e51ad1ce0b4631ebb090abf71631335694661; REALTIME_TRANS_SWITCH=1; FANYI_WORD_SWITCH=1; HISTORY_SWITCH=1; SOUND_SPD_SWITCH=1; SOUND_PREFER_SWITCH=1; hm_lpvt_64ecd82404c51e03dc91cb9e8c025574=1631336626; __yjs_st=2_zTNknDKZoGFJyTzknJLIyTaWyJYZMWi3mJjlMZC3nTrhYZg1nJNLnWvINZA1mDYxMDC2oGMXoGQZm2MWZTQ3oDbjYJc1ZJI4nWNKNDy5mJIXnGvkNJK1YTU5mGIZn2JJnTIWNDE3y2jLnMRMmDjKNMM3mTqZyJRJmWvImZRjyTgYnZeXnZVKnDEYZDM4NTg1mJaYmJMZMTI0ZTE0yTYXoGRHZGQ4NDA3ZDI3mGVMZTq2mJmYZMm0yJCZmTuWzJaWmJzJyZvInZJHODA2yWFIMTe1zMyZmJFMNTM0mTBMzTyWmMjJYMM0mTDMMGq2NZQ3Y2I2oTIZyTRMy2JKMGM4NF83XZE4zWnJZjE5; ab_sr=1.0.1_yTCXMGY3n2mYzJNiYTg4nZVLoTNMmTNioTgxYZg3M2i2MMi2mJhkyZhJyZMZZDa0nZzMnMrHmZRMzDaXNME2nDVmzTDMNTY2ZDA2OTI5mWFIOWY1mDDLnTyznZLMYTY1mTaYZMy3zMIyNDK5NGi3mZfJnJEXZWq5mJRHoWRLOTM4ODC5oGRJODm2NZK5ODI1ZGI5mThHnJFHNDe3YZG2NQ==',
}

data = {
    'from': 'en',
    'to': 'zh',
    'query':'test',
    'transtype':'translang',
    'simple_means_flag':'3',
    'sign':'431039.159886',
    'token':'e7a50e836e217fc147cf54e706e6e4d1',
    'domain':'common',
}

data = urllib.parse.urlencode(data).encode('utf-8')
request = urllib.request.Request(url=url,headers=headers,data=data)
response = urllib.request.urlopen(request)
content = response.read().decode('utf-8')

#print(content)
import json
obj = json.loads(content)
s = json.dumps(obj, ensure_ascii=False)
print(s)